package com.hmall.item.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.ItemDoc;
import com.hmall.item.pojo.PageDTO;
import com.hmall.item.pojo.QueryDto;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements com.hmall.item.service.ItemService {

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageDTO queryPage(Integer page, Integer size) {
        Page<Item> paged = lambdaQuery().orderByDesc(Item::getCreateTime).page(Page.of(page, size));
        return new PageDTO(paged.getTotal(), paged.getRecords());
    }




    @Override
    public void status(Long id, Integer status) {
        lambdaUpdate().eq(Item::getId, id)
                .set(status != null, Item::getStatus, status)
                .update();
    }


    @Override
    public void update1(Item item) {
        lambdaUpdate()
                .set(ObjectUtil.isNotEmpty(item.getName()),Item::getName,item.getName())
                .set(ObjectUtil.isNotEmpty(item.getCategory()),Item::getCategory,item.getCategory())
                .set(ObjectUtil.isNotEmpty(item.getBrand()),Item::getBrand,item.getBrand())
                .set(ObjectUtil.isNotEmpty(item.getPrice()),Item::getPrice,item.getPrice())
                .set(ObjectUtil.isNotEmpty(item.getStock()),Item::getStock,item.getStock())
                .set(ObjectUtil.isNotEmpty(item.getSpec()),Item::getSpec,item.getSpec())
                .set(ObjectUtil.isNotEmpty(item.getIsAD()),Item::getIsAD,item.getIsAD())
                .set(ObjectUtil.isNotEmpty(item.getImage()),Item::getImage,item.getImage())
                .eq(Item::getId,item.getId())
                .update();
    }

    @Override
    public PageDTO<ItemDoc> list0(QueryDto dto) {
        return null;
    }

    @Override
    public Map filters() {

        List<String> category = lambdaQuery().select(Item::getCategory).list().stream().map(Item::getCategory).distinct().collect(Collectors.toList());
        List<String> brand = lambdaQuery().select(Item::getBrand).list().stream().map(Item::getBrand).distinct().collect(Collectors.toList());

        Map map = new HashMap<>();
        map.put("category",category);
        map.put("brand",brand);

        return map;
    }
}
